package com.shaiMiMi;

import java.io.UnsupportedEncodingException;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;



public abstract class ZPackage {

	public String GetPackageType() {
		Class<? extends ZPackage> c = getClass();
		return c.getName();
	}

	public byte[] GetAllSendByte() {
		String xmlString = GetXmlStr();
		byte[] xmlByte = xmlString.getBytes();
		long lSize = 8 + xmlByte.length;
		byte[] sizeByte = DataTypeChangeHelper.longToByte(lSize);

		byte[] allByte = new byte[xmlByte.length+sizeByte.length];
		System.arraycopy(sizeByte, 0, allByte, 0, sizeByte.length);
		System.arraycopy(xmlByte, 0, allByte, sizeByte.length, xmlByte.length);
		return allByte;
	}

	protected Element GetFristElem(Element parentElement) {	//Element的getFirstChild函数很坑爹，有可能返回不是Element
		 NodeList nodeList = parentElement.getChildNodes();
        for(int i=0; i<nodeList.getLength(); i++){
             //判断子节点是否为Element
             if(nodeList.item(i) instanceof Element){
                return ((Element)nodeList.item(i));
            }
        }
        return null;
	}

	protected abstract String GetXmlStr();
	protected abstract void LoadByXml(ZXmlDoc xmlDoc);
}

class ZPackageFactory
{
	private final static String PACK_CLASS_ATTR = "ClassName";

	public static ZPackage CreatePackage(byte[] xmlByteArr){
		String sXmlStr = null;
		try {
			String sTmpXmlStr = new String(xmlByteArr, "UTF-8");
			sXmlStr = new String(sTmpXmlStr.getBytes("UTF-8"), "ISO8859_1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return CreatePackage(sXmlStr);
	}

	public static ZPackage CreatePackage(String sXmlStr){
		try {
			ZXmlDoc xmlDoc = new ZXmlDoc();
			xmlDoc.InitLoadFrom(sXmlStr);
			String packClassName = xmlDoc.GetRootElem().getAttribute(PACK_CLASS_ATTR);
			String sClassPathString = "com.shaiMiMi." + packClassName;
			Class<?> onwClass = Class.forName(sClassPathString);
			ZPackage newPack = (ZPackage)onwClass.newInstance();
			newPack.LoadByXml(xmlDoc);
			return newPack;

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static String GetAllXmlStr(ZPackage pack){
		String string = null;
		return string;
	}
};


class ZPackage_Str extends ZPackage
{
	private String	mStr;

	public ZPackage_Str() {
	}

	public ZPackage_Str(String sStr) {
		mStr = sStr;
	}

	public void SetStr(String sStr) {
		mStr = sStr;
	}

	public String GetStr() {
		return mStr;
	}

	protected String GetXmlStr(){
		ZXmlDoc xmlDoc = new ZXmlDoc();
		xmlDoc.InitSaveTo();
		xmlDoc.AppendChild(xmlDoc.GetRootElem(), "strVal", mStr);
		return xmlDoc.GetXmlStr(this);
	}

	protected void LoadByXml(ZXmlDoc xmlDoc){
		Element rootElement = xmlDoc.GetRootElem();
		Element fristElem = GetFristElem(rootElement);
		mStr = xmlDoc.GetStrVal(fristElem);
	}
};

class ZPackage_ByteArr extends ZPackage
{
	public void SetByteArr(byte[] ByteArr){
		mByteArr = ByteArr;
	}

	byte[] GetByteArr(){
		return mByteArr;
	}

	protected String GetXmlStr(){
		ZXmlDoc xmlDoc = new ZXmlDoc();
		xmlDoc.InitSaveTo();
		xmlDoc.AppendChild(xmlDoc.GetRootElem(), "byteArrVal", mByteArr);
		return xmlDoc.GetXmlStr(this);
	}

	protected void LoadByXml(ZXmlDoc xmlDoc){
		Element rootElem = xmlDoc.GetRootElem();
		Element elem = GetFristElem(rootElem);
		mByteArr = xmlDoc.GetVal(elem);
	}

	private byte[]	mByteArr;
};

